<template>
  <ContentWrap>
    <el-descriptions :column="1" border>
      <el-descriptions-item label="订单单号">
        {{ detailData.no }}
      </el-descriptions-item>
      <el-descriptions-item label="采购日期">
        <el-date-picker
          :disabled="true"
          v-model="detailData.orderTime"
          type="date"
          value-format="x"
          placeholder="选择采购日期"
          class="!w-1/1"
        />
      </el-descriptions-item>
      <el-descriptions-item label="供应商">
        <el-select
          v-model="detailData.supplierId"
          clearable
          filterable
          placeholder="请选择供应商"
          class="!w-1/1"
          :disabled="true"
        >
          <el-option
            v-for="item in supplierList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-descriptions-item>
      <el-descriptions-item label="备注">
        {{ detailData.remark }}
      </el-descriptions-item>
      <el-descriptions-item label="附件">
        <UploadFile :is-show-tip="false" v-model="detailData.fileUrl" :limit="1" :disabled="true" />
      </el-descriptions-item>
      <el-descriptions-item label="订单样本清单">
        <el-tabs v-model="subTabsName" class="-mt-15px -mb-10px">
          <el-tab-pane label="订单样本清单" name="item">
            <PurchaseOrderItemForm ref="itemFormRef" :items="detailData.items" :disabled="true" />
          </el-tab-pane>
        </el-tabs>
      </el-descriptions-item>
      <el-descriptions-item label="优惠率（%）">
        {{ detailData.discountPercent }}
      </el-descriptions-item>
      <el-descriptions-item label="税率（%）">
        {{ detailData.taxPercent }}
      </el-descriptions-item>
    </el-descriptions>
  </ContentWrap>
</template>
<script lang="ts" setup>
import { DICT_TYPE } from '@/utils/dict'
import { propTypes } from '@/utils/propTypes'
import { SupplierApi, SupplierVO } from '@/api/erp/purchase/supplier'
import { PurchaseOrderApi, PurchaseOrderVO } from '@/api/erp/purchase/applyOrder'
import PurchaseOrderItemForm from './components/PurchaseOrderItemForm.vue'

defineOptions({ name: 'SupplierDetail' })

const { query } = useRoute() // 查询参数

const props = defineProps({
  id: propTypes.string.def(undefined)
})
const detailLoading = ref(false) // 表单的加载中
const detailData = ref<any>({}) // 详情数据
const queryId = query.id as unknown as number // 从 URL 传递过来的 id 编号

/** 获得数据 */
const getInfo = async () => {
  detailLoading.value = true
  try {
    detailData.value = await PurchaseOrderApi.getPurchaseOrder(props.id || queryId)
  } finally {
    detailLoading.value = false
  }
}
defineExpose({ open: getInfo }) // 提供 open 方法，用于打开弹窗

/** 获取供应商列表 */
const supplierList = ref<SupplierVO[]>([]) // 供应商列表
const getSupplierList = async () => {
  supplierList.value = await SupplierApi.getSupplierSimpleList()
}
/** 默认显示的子表 */
const subTabsName = ref('item')
/** 初始化 **/
onMounted(() => {
  getInfo()
  getSupplierList()
})
</script>
